package com.xiezc.model;

import com.xiezc.Xutil;
import lombok.EqualsAndHashCode;
import lombok.EqualsAndHashCode.Exclude;
import lombok.Synchronized;
import lombok.ToString;

import java.net.InetSocketAddress;

/**
 * @author wb-xzc291800
 * @date 2018/08/24 14:57
 */
@ToString
@EqualsAndHashCode
public class Device {

    Long id;

    @Exclude
    InetSocketAddress address;

    @Exclude
    boolean conneted;

    /**
     * 与这个设备的通信令牌, 由被请求者通过验证后分配给请求者的. 之后的每次请求都要带上这个参数
     */
    @Exclude
    long token;

    @Exclude
    long time = System.currentTimeMillis();

    @Synchronized
    public Long getId() {

        return id;
    }

    @Synchronized
    public void setId(Long id) {

        this.id = id;
    }

    @Synchronized
    public InetSocketAddress getAddress() {

        return address;
    }

    @Synchronized
    public void setAddress(InetSocketAddress address) {

        refreshTime();
        this.address = address;
    }

    @Synchronized
    public Boolean getConneted() {

        return conneted;
    }

    @Synchronized
    public void setConneted(Boolean conneted) {

        this.conneted = conneted;
    }

    @Synchronized
    public long getTime() {

        return time;
    }

    @Synchronized
    public void refreshTime() {

        this.time = System.currentTimeMillis();
    }

    @Synchronized
    public long getToken() {

        return token;
    }

    @Synchronized
    public void setToken(long token) {

        this.token = token;
    }

    @Synchronized
    public long getAndRefreshToken() {

        long token = this.token;
        this.token = Xutil.random.nextInt(10000);
        return token;
    }

}
